import '@/config'
import { createRouter, createWebHistory } from 'vue-router'
import views from '__config/view'
import { userStore } from '@/store/user'
import { menuStore } from '__store/menu'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'main',
      component: () => import('@/views/main/index.vue'),
      children: views.getContents()
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue')
    }
  ]
})

router.beforeEach((to, _, next) => {
  console.log(to.name)
  if (!to.name) {
    next('/')
  } else if (to.name == 'login') {
    next()
  } else if (!userStore().isLogin) {
    console.log('111')
    next('/login')
  } else {
    next()
  }
})

router.goRoot = (name, query) => {
  console.log('goRoot', name, query, menuStore().first)
  // 跳转根菜单
  menuStore().choiceRoot()
  if (name) {
    router.push({ name, query })
  } else {
    console.log(menuStore().first.name)
    router.replace({ name: menuStore().first.name })
  }
}

router.goLogin = () => {
  router.replace({ name: 'login' })
}

export default router
